import { createApp } from 'vue'
import './style.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import store from './store/index'
import App from './App.vue'
import router from './router/index'
import PanelHeader from './components/panelHeader.vue'



//刷新后的动态路由添加
const localData = localStorage.getItem('pz_dami')
if (localData) {
    store.commit('dynamicMenu', JSON.parse(localData).menu.routerList)
    store.state.menu.routerList.forEach(item => {
        router.addRoute('main', item)
    })
}
//路由守卫
router.beforeEach((to, from) => {
    const token = localStorage.getItem('pz_token')
    //非登录页面token不存在
    if (!token && to.path !== '/login') {
        return '/login'
    } else if (token && to.path === '/login') {
        return '/'
    }
    else {
        return true
    }
})

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.component('PanelHeader', PanelHeader)
app.use(router)
app.use(store)
app.use(ElementPlus)
app.use(ElementPlus, {
    locale: zhCn,
})
app.mount('#app')